#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BIT_WIDTH 32

// this is the prototype for the dec2bin function
void dec2bin(unsigned int x, char bin[]);
// this is the prototype for the dec2hex function
void dec2hex(unsigned int x, char hex[]);

/*
 * Converts an unsigned integer into a binary string
 * (represented by an array of '1' and '0' char's)
 *
 * x: the integer to conver
 * bin[]: the character array that will hold the result
 */
void dec2bin(unsigned int x, char bin[])
{
	//The result of the previous result divided by 2.
	int currentResult;
	//The result before is was divided by 2 to get the current result.
	int previousResult;
	//The remainder of the previous result divided by 2.
	int remainder;
	// generic iteration index
	int i;
	
	//initializing variables
	currentResult = 0;
	previousResult = x;
	remainder = 0;

	//Populates the bin array
	for(i = 31; i > -1; i--){	
		currentResult = previousResult/2;
		remainder = previousResult % 2;
		previousResult = currentResult;
		bin[i] = remainder + 48;			
	}

	//Prints out the bin array
	for(i = 0; i < 32; i++){
		printf("%c", bin[i]);
	}
	
}

/*
 *  Converts an unsigned integer into a hexadecimal string.
 * 
 *  x: the integer to convert
 *  hex[]: the character array that will hold the result
 */
void dec2hex(unsigned int x, char hex[])
{
	//An array of the hex digits (0-F)
	char hexValues[17];

	// generic iteration index
	int i;

	// generic iteration index
	int j;

	//The decimal value indicating the beginning of 0-9 on the ASCII table
	int numberStart;

	//The decimal value indicating the beginning of A-F on the ASCII table
	int letterStart;
	
	//The number being divided by 16 to get the hex digits
	int decimal;

	//The remainder of the decimal value divided by 16 that gives us the hex digits.
	int remainder;

	//initializing variables
	i = 0;
	numberStart = 48;
	letterStart = 65;

	//Populates the hexValues array with the number hex digits
	for(numberStart; numberStart < 58; numberStart++){
		hexValues[i] = numberStart;
		i++;
	}
	//Populates the hexValues array with the letter hex digits
	for(letterStart; letterStart < 71; letterStart++){
		hexValues[i] = letterStart;
		i++;
	}

	//If the decimal to be converted is zero then the hex array is filled with zeros.
	if(x == 0){
		for(i = 0; i < 8; i++){
			hex[i] = 48; 
		}
	}
	//If the decimal to be converted is not zero then it is converted.
	else{
		decimal = x;
		for(i = 7; i > -1; i--){
			//Gets the value of the hex digit
			remainder = decimal % 16;
			//Converts the hex digit from it's decimal to hexidecimal value (ex: 10 to A)
			for(j = 0; j < 16; j++){
				if(hexValues[j] == remainder + 48 && remainder < 10){
					hex[i] = hexValues[j];
				}
				else if(hexValues[j] == remainder + 55 && remainder > 9){
					hex[i] = hexValues[j];
				}
			}
			decimal = decimal/16;
		}
	}
	//The hex array is printed.
	for(i = 0; i < 8; i++){
		printf("%c", hex[i]);
	}
}

int main()
{
	// holds the binary representation
	char bin[BIT_WIDTH+1]; // the "+1" is for null-terminating the array so it acts as a string

	// 'b' for dec-to-bin conversion; 'h' for dec-to-hex conversion
	char op;

	// our decimal value
	unsigned int dec;

	// holds the hex representation
	char hex[BIT_WIDTH/4+1];

	// the number of conversions listed in our file
	unsigned int numConversions;

	// input file handle
	FILE* fin;

	// generic iteration index
	int i;

	// open the input file
	fin = fopen("input.txt", "r");

	// read in the number of conversions to perform
	fscanf(fin, "%u\n", &numConversions);

	// iterate over all conversions
	for(i=0; i<numConversions; i++)
	{
		// read in the conversion type and decimal number
		fscanf(fin, "%c,%u\n", &op, &dec);

		// decide if this is a decimal-->binary or decimal-->hexadecimal conversion
		if(op == 'b'){
			// perform the conversion
			dec2bin(dec, bin);
			printf("\n");
		}
		else{
			printf("0x");
			dec2hex(dec, hex);
			printf("\n");
		}
		
		// print out the result (don't forget the newline! don't forget "0x" in front of your hex values!)
	}

	// close the file
	fclose(fin);

	// return success
	return 0;
}